﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpaceInvaders
{
    public static class GerenciadorDeInimigo
    {
        //public static List<Inimigo> enemies = new List<Inimigo>();
        public static Inimigo[] enemies;

        static Random r = new Random();

        static int rEnemy;
        static int shootDelay = 20;
        static int Timer = 0;

        public static float speedX = 1;
        public static float speedY = 10;
        static bool moveDown = false;

        public static void Add(Inimigo enemy)
        {
            enemies = Inimigo.AddToList(enemies, enemy);
        }

        public static void Update()
        {
            Timer++;

            if (enemies != null)
            {
                for (int n = 0; n < enemies.Length; n++)
                {
                    if (enemies[n].CollisionRect().X + enemies[n].CollisionRect().Width >= 800 || enemies[n].CollisionRect().X <= 0)
                    {
                        speedX *= -1;
                        moveDown = true;
                        break;
                    }
                }
                for (int n = 0; n < enemies.Length; n++)
                {
                    if (moveDown)
                        enemies[n].Move(speedX, speedY);
                    else
                        enemies[n].Move(speedX, 0);
                }

                moveDown = false;

                if (Timer >= shootDelay)
                {
                    rEnemy = r.Next(enemies.Length);
                    GerenciadorDeTiro.Add(enemies[rEnemy].position.X, enemies[rEnemy].position.Y, enemies[rEnemy].texture.Width, enemies[rEnemy].texture.Height, true);
                    Timer = 0;
                    shootDelay = r.Next(15, 50);
                }
            }
        }

        public static void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            if(enemies != null)
                for (int n = 0; n < enemies.Length; n++)
                {
                    enemies[n].Draw(spriteBatch);
                }
        }
    }
}
